fun printInt (a:int) =
    print(Int.toString(a)^" ");

fun getInt () =
    Option.valOf (TextIO.scanStream (Int.scan StringCvt.DEC) TextIO.stdIn);
    
fun printIntList ( [] ) = ()
  | printIntList ( x::xs ) = 
    let
	val tmp = printInt(x)
    in
	printIntList(xs)
    end;

fun getIntList ( 0 ) = []
  | getIntList ( N:int) = getInt()::getIntList(N-1);

(*  完成Begin和End间代码 *)    
(*****Begin*****)
fun add(_,0)=0
|add([],i)=0
|add(x::L,1)=x
|add(x::L,i)=x+add(L,i-1);


fun PrefixSum A = 
    let
        fun sumList [] = 0
          | sumList (x::xs) = x + sumList xs

        fun computePrefixSum ([], _) = []
          | computePrefixSum (x::xs, i) = 
            let
                val currentSum = add(A,i)
            in
                currentSum :: computePrefixSum(xs, i+1)
            end
    in
        computePrefixSum(A, 1)
    end;

fun fastPrefixSum A = 
    let
        fun fastPrefixSumHelper ([], _) = []
          | fastPrefixSumHelper (x::xs, acc) = 
            let
                val currentSum = x + acc
                val rest = fastPrefixSumHelper(xs, currentSum)
            in
                currentSum :: rest
            end
    in
        fastPrefixSumHelper(A, 0)
    end;
(*****End*****)

val L = getIntList(3);
printIntList (PrefixSum L);
printIntList (fastPrefixSum L);

